package com.project.slsx.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.bean.OpenCardBean;
import com.project.slsx.utils.DialogUtils;
import com.project.slsx.utils.DisplayUtils;
import com.project.slsx.utils.ParamsUtils;
import com.project.slsx.widget.LineTextView;
import com.project.slsx.widget.dialog.BaseNiceDialog;
import com.project.slsx.widget.dialog.LoadingView;
import com.project.slsx.widget.dialog.ViewConvertListener;
import com.project.slsx.widget.dialog.ViewHolder;

import java.util.ArrayList;

public class OpenCardActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = OpenCardActivity.class.getSimpleName();
    private String vipId = "";
    private LoadingView loadingView;
    private EditText cardId;
    private EditText number;
    private EditText name;
    private EditText mobile;
    private EditText password1;
    private EditText password2;
    private EditText money;

    private String cardIdStr;
    private String numberStr;
    private String nameStr;
    private String mobileStr;
    private String password1Str;
    private String password2Str;
    private String moneyStr;
    private ArrayList<EditText> editTexts;

    @Override
    public int getLayoutId() {
        return R.layout.activity_open_card;
    }

    @Override
    public void initView() {
        findViewById(R.id.rl_back).setOnClickListener(this);
        TextView title = findViewById(R.id.tv_title);
        title.setText("会员开卡");
        TextView right = findViewById(R.id.tv_right);
        right.setText("收银");

        LineTextView tv1 = findViewById(R.id.ltv1);
        LineTextView tv2 = findViewById(R.id.ltv2);
        LineTextView tv3 = findViewById(R.id.ltv3);
        LineTextView tv4 = findViewById(R.id.ltv4);
        LineTextView tv5 = findViewById(R.id.ltv5);
        LineTextView tv6 = findViewById(R.id.ltv6);
        LineTextView tv7 = findViewById(R.id.ltv7);

        tv1.setText("会员卡ID");
        tv2.setText("会员卡号");
        tv3.setText("持卡人姓名");
        tv4.setText("手机号码");
        tv5.setText("会员卡密码");
        tv6.setText("重复密码");
        tv7.setText("充值金额");

        findViewById(R.id.btn_open_saoma).setOnClickListener(this);
        findViewById(R.id.btn_open_cash).setOnClickListener(this);
        cardId = findViewById(R.id.ed_open_cardid);
        number = findViewById(R.id.ed_open_number);
        name = findViewById(R.id.ed_open_name);
        mobile = findViewById(R.id.ed_open_mobile);
        password1 = findViewById(R.id.ed_open_password1);
        password2 = findViewById(R.id.ed_open_password2);
        money = findViewById(R.id.ed_open_money);


        cardId.addTextChangedListener(new IdWatcher());
        number.addTextChangedListener(new NumberWatcher());
        name.addTextChangedListener(new NameWatcher());
        mobile.addTextChangedListener(new MobileWatcher());
        password1.addTextChangedListener(new Passowrd1Watcher());
        password2.addTextChangedListener(new Passowrd2Watcher());
        money.addTextChangedListener(new MoneyWatcher());

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            vipId = intent.getStringExtra(Constans.VIP_ID);
            Log.i(TAG, "initData: " + vipId);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_open_saoma:

                if (TextUtils.isEmpty(cardIdStr)) {
                    showToast(mContext, "请输入会员id");
                    return;
                }
                if (TextUtils.isEmpty(numberStr)) {
                    showToast(mContext, "请输入会员卡卡号");
                    return;
                }
                if (TextUtils.isEmpty(nameStr)) {
                    showToast(mContext, "请输入持卡人姓名");
                    return;
                }
                if (TextUtils.isEmpty(mobileStr)) {
                    showToast(mContext, "请输入会手机号码");
                    return;
                }
                if (TextUtils.isEmpty(password1Str)) {
                    showToast(mContext, "请输入会员卡密码");
                    return;
                }
                if (TextUtils.isEmpty(password2Str)) {
                    showToast(mContext, "请再次输入密码");
                    return;
                }
                if (TextUtils.isEmpty(moneyStr)) {
                    showToast(mContext, "充值金额");
                    return;
                }

                if (!password1Str.equals(password2Str)) {
                    showToast(mContext, "两次密码输入不一致");
                }

                DialogUtils.showDialog(R.layout.dialog_open_saoma,
                        DisplayUtils.dp2px(mContext, 200),
                        DisplayUtils.dp2px(mContext, 180),
                        true,
                        openListener,
                        getSupportFragmentManager());
                break;
            case R.id.btn_open_cash:

                break;
        }
    }

    private ViewConvertListener openListener = new ViewConvertListener() {
        @Override
        protected void convertView(ViewHolder holder, BaseNiceDialog dialog) {
            LinearLayout view = (LinearLayout) holder.getConvertView();
            EditText edSaoma = view.findViewById(R.id.ed_barcode);
            Button btnCancel = view.findViewById(R.id.btn_open_cancel);
            Button btnOK = view.findViewById(R.id.btn_open_ok);
            btnCancel.setOnClickListener(v -> DialogUtils.dissmissDialog());

            btnOK.setOnClickListener(v -> doSaoMaPay());
        }


    };

    private ViewConvertListener loadlistener = new ViewConvertListener() {
        @Override
        protected void convertView(ViewHolder holder, BaseNiceDialog dialog) {
            LinearLayout loading = (LinearLayout) holder.getConvertView();
            loadingView = loading.findViewById(R.id.loadingview);
        }
    };

    /**
     * 扫码付款
     */
    private void doSaoMaPay() {
        HttpParams vipaddParams = ParamsUtils.getVipaddParams();
        vipaddParams.put("card_id", cardIdStr);
        vipaddParams.put("vip_number", numberStr);
        vipaddParams.put("name", nameStr);
        vipaddParams.put("mobile", mobileStr);
        vipaddParams.put("password", password1Str);
        vipaddParams.put("money", moneyStr);
        vipaddParams.put("type", "2");
        vipaddParams.put("code", "134684575414063255");

        OkGo.<String>post(Constans.BASEURL).tag("vipadd").params(vipaddParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                OpenCardBean resultBean = JSONObject.parseObject(response.body(), OpenCardBean.class);
                Toast.makeText(mContext, resultBean.getErrmsg(), Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.putExtra("opencardresult", resultBean);
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
            }
        });
    }

    /**
     * 扫码充值
     */
    private void doChongzhiSaoma() {
        DialogUtils.showLoading(loadlistener, getSupportFragmentManager());
        HttpParams viprechargeParams = ParamsUtils.getViprechargeParams();
        viprechargeParams.put("id", vipId);
        viprechargeParams.put("amount", 0.01);
        viprechargeParams.put("remark", "");
        viprechargeParams.put("type", 2);
        viprechargeParams.put("code", "134642904298903221");
        OkGo.<String>post(Constans.BASEURL).tag("chongzhi").params(viprechargeParams).execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                if (loadingView != null) {
                    loadingView.showLoading();
                    loadingView.setText("正在充值...");
                }
            }

            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                OpenCardBean openCardBean = JSONObject.parseObject(response.body(), OpenCardBean.class);
                if (openCardBean.getErrno() == Constans.SUCCESS) {
                    loadingView.showSuccess();
                    loadingView.setText(openCardBean.getErrmsg());
                    showToast(mContext, openCardBean.getErrmsg());
                } else {
                    loadingView.showFail();
                    loadingView.setText("充值失败!");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingView.showFail();
                loadingView.setText("充值失败!");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.dissmissDialog();
                    }
                }, 3000);
            }
        });
    }

    //==============================================================
    private class IdWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            cardIdStr = s.toString().trim();
        }
    }

    private class NumberWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            numberStr = s.toString().trim();
        }
    }//==

    private class NameWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            nameStr = s.toString().trim();
        }
    }

    private class MobileWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mobileStr = s.toString().trim();
        }
    }

    private class Passowrd1Watcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            password1Str = s.toString().trim();
        }
    }

    private class Passowrd2Watcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            password2Str = s.toString().trim();
        }
    }

    private class MoneyWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            moneyStr = s.toString().trim();
        }
    }
}
